package ui.label;

import utils.PropertiesUtils;

import javax.swing.*;
import java.awt.*;

/**
 * @version V1.0
 * @Title: 重写JLabel，使其能够改变透明度
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class LimpidLabel extends JLabel {

    public PropertiesUtils settings = new PropertiesUtils("SETTINGS.properties");

    public LimpidLabel(ImageIcon icon, int x, int y, int width, int height){
        super.setIcon(icon);
        super.setBounds(x, y, width, height);
    }

    public LimpidLabel(ImageIcon icon){
        super.setIcon(icon);
    }

    private AlphaComposite cmp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1);
    private float alpha = 0.3f;

    public void setAlpha(float alpha) {
        this.alpha = alpha;
        if (isVisible())   paintImmediately(getBounds());
    }

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        alpha = Float.parseFloat(settings.get("alpha"));
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(cmp.derive(alpha));
        super.paintComponent(g2d);
    }

}
